//读文件打印脚本
new File('../haiku.txt').eachLine { line -> println line }

//读文件打印及打印行号脚本
baseDir = '..'
new File(baseDir, 'haiku.txt').eachLine { line, nb ->
    println "Line $nb: $line"
}

//当然了，有时候你可能更加喜欢用Reader来操作，使用Reader时即使抛出异常也会自动关闭IO。如下：
/*
def count = 0, MAXSIZE = 3
new File(baseDir,"haiku.txt").withReader { reader ->
	while (reader.readLine()) {
		if (++count > MAXSIZE) {
			throw new RuntimeException('Haiku should only have 3 verses')
		}
	}
}*/

//把读到的文件行内容全部存入List列表中
def list = new File(baseDir, 'haiku.txt').collect { it }
//把读到的文件行内容全部存入String数组列表中
def array = new File(baseDir, 'haiku.txt') as String[]
////把读到的文件内容全部转存为byte数组
//byte[] contents = file.bytes

//把读到的文件转为InputStream，切记此方式需要手动关闭流
def is = new File(baseDir, 'haiku.txt').newInputStream()
// do something ...
is.close()

//把读到的文件以InputStream闭包操作，此方式不需要手动关闭流
new File(baseDir, 'haiku.txt').withInputStream { stream ->
    // do something ...
}

